package com.vainycos.lotterysearch.tool;

import com.vainycos.lotterysearch.model.BigLotteryBall;
import com.vainycos.lotterysearch.model.DoubleColorBall;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: Vainycos
 * @description 获取彩票开奖信息工具类
 * @date: 2022/11/3 13:42
 */
public class GetLottery {
    //大乐透请求url
    private static String bigUrl = "http://datachart.500.com/dlt/history/newinc/history.php?limit=10000&sort=0";
    //双色球请求url
    private static String douUrl = "http://datachart.500.com/ssq/history/newinc/history.php?limit=100000&sort=0";
    //匹配开奖正则
    private static String listRegex = "--><(.*?)<\\/tr>";
    //匹配开奖明细正则
    private static String infoRegex = ">[,\\d-]+?<";

    /**
     * 获取全部双色球的开奖信息数据
     *
     * @return list对象
     * @throws Exception
     */
    @SneakyThrows
    public static List<DoubleColorBall> getAllDCB() {
        List<DoubleColorBall> list = new ArrayList<DoubleColorBall>();
        //获取url信息
        String data = GetUrlData.getString(GetLottery.douUrl);

        Pattern pattern = Pattern.compile(GetLottery.listRegex);
        Matcher matcher = pattern.matcher(data);
        //遍历获取的数据
        while (matcher.find()) {
            String info = matcher.group();
            List<String> infoData = new ArrayList<String>();
            Pattern patternInfo = Pattern.compile(GetLottery.infoRegex);
            Matcher matcherInfo = patternInfo.matcher(info);
            //获取具体的数据
            while (matcherInfo.find()) {
                infoData.add(matcherInfo.group().replaceAll(">|<|,", ""));
            }
            //创建对象
            list.add(new DoubleColorBall(infoData));
        }
        return list;

    }

    public static void main(String[] args) throws Exception {
        //获取url信息
        String data = GetUrlData.getString("https://datachart.500.com/pls/?shujcount=0&sort=0&expect=30");
        System.out.println(data);
    }

    /**
     * 根据传进来的的开奖期号获取这个开奖期号自后的开奖信息
     *
     * @param period_Id 开奖期号
     * @return List对象
     * @throws Exception
     */
    public static List<DoubleColorBall> getBeforeDCB(int period_Id) throws Exception {
        List<DoubleColorBall> list = new ArrayList<DoubleColorBall>();
        //获取url信息
        String data = GetUrlData.getString(GetLottery.douUrl);

        Pattern pattern = Pattern.compile(GetLottery.listRegex);
        Matcher matcher = pattern.matcher(data);
        //遍历获取的数据
        while (matcher.find()) {
            String info = matcher.group();
            List<String> infoData = new ArrayList<String>();
            Pattern patternInfo = Pattern.compile(GetLottery.infoRegex);
            Matcher matcherInfo = patternInfo.matcher(info);
            //获取具体的数据
            while (matcherInfo.find()) {
                infoData.add(matcherInfo.group().replaceAll(">|<|,", ""));
            }
            //要是获取的期号小于或者等于传进来的期号则跳出
            if (Integer.parseInt(infoData.get(0)) < period_Id) {
                break;
            } else {
                //创建对象
                list.add(new DoubleColorBall(infoData));
            }
        }
        return list;
    }

    /**
     * 根据传进来的的开奖期号获取这个开奖期号自后的开奖信息
     *
     * @param period_Id 开奖期号
     * @return List对象
     * @throws Exception
     */
    @SneakyThrows
    public static List<DoubleColorBall> getLaterDCB(int period_Id) {
        List<DoubleColorBall> list = new ArrayList<DoubleColorBall>();
        //获取url信息
        String data = GetUrlData.getString(GetLottery.douUrl);
        Pattern pattern = Pattern.compile(GetLottery.listRegex);
        Matcher matcher = pattern.matcher(data);
        //遍历获取的数据
        while (matcher.find()) {
            String info = matcher.group();
            List<String> infoData = new ArrayList<String>();
            Pattern patternInfo = Pattern.compile(GetLottery.infoRegex);
            Matcher matcherInfo = patternInfo.matcher(info);
            //获取具体的数据
            while (matcherInfo.find()) {
                infoData.add(matcherInfo.group().replaceAll(">|<|,", ""));
            }
            //要是获取的期号大于传进来的期号符合条件 否则跳出
            if (Integer.parseInt(infoData.get(0)) > period_Id) {
                list.add(new DoubleColorBall(infoData));
            } else {
                break;
            }
        }
        return list;
    }

    /**
     * 查询双色球单期开奖情况
     *
     * @param period_Id 开奖期号
     * @return List对象
     * @throws Exception
     */
    public static DoubleColorBall getDCBBySinglePeriodId(int period_Id) throws Exception {
        DoubleColorBall doubleColorBall= new DoubleColorBall();
        //获取url信息
        String data = GetUrlData.getString(GetLottery.douUrl);

        Pattern pattern = Pattern.compile(GetLottery.listRegex);
        Matcher matcher = pattern.matcher(data);
        //遍历获取的数据
        while (matcher.find()) {
            String info = matcher.group();
            List<String> infoData = new ArrayList<String>();
            Pattern patternInfo = Pattern.compile(GetLottery.infoRegex);
            Matcher matcherInfo = patternInfo.matcher(info);
            //获取具体的数据
            while (matcherInfo.find()) {
                infoData.add(matcherInfo.group().replaceAll(">|<|,", ""));
            }
            //要是获取的期号小于或者等于传进来的期号则跳出
            if (Integer.parseInt(infoData.get(0)) == period_Id) {
                doubleColorBall = new DoubleColorBall(infoData);
                break;
            }
        }
        return doubleColorBall;
    }

    /**
     * 查询双色球单期开奖情况
     *
     * @param period_Id 开奖期号
     * @return List对象
     * @throws Exception
     */
    public static DoubleColorBall searchLatestDoubleColor() throws Exception {
        DoubleColorBall doubleColorBall= new DoubleColorBall();
        //获取url信息
        String data = GetUrlData.getString(GetLottery.douUrl);

        Pattern pattern = Pattern.compile(GetLottery.listRegex);
        Matcher matcher = pattern.matcher(data);
        //遍历获取的数据
        while (matcher.find()) {
            String info = matcher.group();
            List<String> infoData = new ArrayList<String>();
            Pattern patternInfo = Pattern.compile(GetLottery.infoRegex);
            Matcher matcherInfo = patternInfo.matcher(info);
            //获取具体的数据
            while (matcherInfo.find()) {
                infoData.add(matcherInfo.group().replaceAll(">|<|,", ""));
            }
            doubleColorBall = new DoubleColorBall(infoData);
            break;
        }
        return doubleColorBall;
    }

    /**
     * 获取全部大乐透的开奖信息
     *
     * @return List对象
     * @throws Exception
     */
    @SneakyThrows
    public static List<BigLotteryBall> getAllBLB()  {
        List<BigLotteryBall> list = new ArrayList<BigLotteryBall>();
        //获取url信息
        String data = GetUrlData.getString(GetLottery.bigUrl);

        Pattern pattern = Pattern.compile(GetLottery.listRegex);
        Matcher matcher = pattern.matcher(data);
        //遍历获取的数据
        while (matcher.find()) {
            String info = matcher.group();
            List<String> infoData = new ArrayList<String>();
            Pattern patternInfo = Pattern.compile(GetLottery.infoRegex);
            Matcher matcherInfo = patternInfo.matcher(info);
            //获取具体的数据
            while (matcherInfo.find()) {
                infoData.add(matcherInfo.group().replaceAll(">|<|,", ""));
            }
            //创建对象
            list.add(new BigLotteryBall(infoData));
        }
        return list;

    }

    /**
     * 根据传入的期号来获取这个期号之后的开奖信息
     *
     * @param period_Id 开奖期号
     * @return
     * @throws Exception
     */
    @SneakyThrows
    public static List<BigLotteryBall> getLaterBLB(int period_Id) {
        List<BigLotteryBall> list = new ArrayList<BigLotteryBall>();
        //获取url信息
        String data = GetUrlData.getString(GetLottery.bigUrl);

        Pattern pattern = Pattern.compile(GetLottery.listRegex);
        Matcher matcher = pattern.matcher(data);
        //遍历获取的数据
        while (matcher.find()) {
            String info = matcher.group();
            List<String> infoData = new ArrayList<String>();
            Pattern patternInfo = Pattern.compile(GetLottery.infoRegex);
            Matcher matcherInfo = patternInfo.matcher(info);
            //获取具体的数据
            while (matcherInfo.find()) {
                infoData.add(matcherInfo.group().replaceAll(">|<|,", ""));
            }
            //要是获取的期号大于传进来的期号则符合 否则跳出
            if (Integer.parseInt(infoData.get(0)) > period_Id) {
                list.add(new BigLotteryBall(infoData));
            } else {
                break;
            }
        }
        return list;
    }

    /**
     * 根据传入的期号来获取这个期号的开奖信息
     *
     * @param period_Id 开奖期号
     * @return
     * @throws Exception
     */
    public static BigLotteryBall getBLBBySinglePeriodId(int period_Id) throws Exception {
        BigLotteryBall bigLottoBall = new BigLotteryBall();
        //获取url信息
        String data = GetUrlData.getString(GetLottery.bigUrl);

        Pattern pattern = Pattern.compile(GetLottery.listRegex);
        Matcher matcher = pattern.matcher(data);
        //遍历获取的数据
        while (matcher.find()) {
            String info = matcher.group();
            List<String> infoData = new ArrayList<String>();
            Pattern patternInfo = Pattern.compile(GetLottery.infoRegex);
            Matcher matcherInfo = patternInfo.matcher(info);
            //获取具体的数据
            while (matcherInfo.find()) {
                infoData.add(matcherInfo.group().replaceAll(">|<|,", ""));
            }
            //要是获取的期号等于传进来的期号则返回 待改进
            if (Integer.parseInt(infoData.get(0)) == period_Id) {
                bigLottoBall = new BigLotteryBall(infoData);
                break;
            }
        }
        return bigLottoBall;
    }

    /**
     * 查询最近一期开奖情况
     *
     * @return
     * @throws Exception
     */
    public static BigLotteryBall searchLatestBigLottery() throws Exception {
        BigLotteryBall bigLottoBall = new BigLotteryBall();
        //获取url信息
        String data = GetUrlData.getString(GetLottery.bigUrl);

        Pattern pattern = Pattern.compile(GetLottery.listRegex);
        Matcher matcher = pattern.matcher(data);
        //遍历获取的数据
        while (matcher.find()) {
            String info = matcher.group();
            List<String> infoData = new ArrayList<String>();
            Pattern patternInfo = Pattern.compile(GetLottery.infoRegex);
            Matcher matcherInfo = patternInfo.matcher(info);
            //获取具体的数据
            while (matcherInfo.find()) {
                infoData.add(matcherInfo.group().replaceAll(">|<|,", ""));
            }
            bigLottoBall = new BigLotteryBall(infoData);
            break;
        }
        return bigLottoBall;
    }
}